<?php

class FormTestController extends Mac_Controller {
    
    public function formulaireAction() {
        $form = new Form('myForm', array(
            'action' => '/form-test/formulaire',
            'class' => 'formul'
        ));

        $text = new Form_Element_Text('text');
        $text->addValidator(new Form_Validator_Email())
                ->setRequired();
        
        $radio1 = new Form_Element_Radio('radio1', 'radio', 1, 'Radio 1');
        $radio2 = new Form_Element_Radio('radio2', 'radio', 2, 'Radio 2');
        $radio1->setRequired(true);

        $cb = new Form_Element_Checkbox('cb', 'cb_seule', 'ok', 'Checkbox');
        $cb->setRequired(true);

        $select = new Form_Element_Select('ingredient');
        $select
            ->setEmptyByDefault()
            ->setOptions(array(
            'jambon' => 'Jambon',
            'fromage' => 'Fromage',
            'tomate' => 'Tomate'
        ), 'Salé')->setOptions(array(
            'ananas' => 'Ananas',
            'nutella' => 'Nutella'
        ), 'Sucré')
                ->addOption('champignon', 'Champignon', 'Salé')
                ->addOption('patate', 'Patate')
                ->setRequired(false)
                ->setSize(5)
                ->setMultiple(true);

        $pass = new Form_Element_Password('pass');
        $pass->setRequired(true);

        $button = new Form_Element_Button('button', 'Bouton inutile');

        $hidden = new Form_Element_Hidden('hidden', 'toto');

        $valid = new Form_Element_Submit('valider', 'Valider');

        $image = new Form_Element_Image('img_valid', '/image/valider.jpg', 'Valider');

        $cb1 = new Form_Element_Checkbox('cb1', 'cb', 'val1', 'Checkbox 1', true);
        $cb2 = new Form_Element_Checkbox('cb2', 'cb', 'val2', 'Checkbox 2', true);
        $cb3 = new Form_Element_Checkbox('cb3', 'cb', 'val3', 'Checkbox 3', true);
        $cb1->setRequired(true);

        $area = new Form_Element_Textarea('area');
        $area->setSize(5, 35)->setRequired();
        
        $form->addElements(array(
            $text,
            $radio1,
            $radio2,
            $cb,
            $cb1,
            $cb2,
            $cb3,
            $select,
            $pass,
            $button,
            $hidden,
            $image,
            $area,
            $valid
        ));
        
        if ($this->getRequest()->isPostRequest()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $this->view->result = 'oui';
            } else {
                $this->view->result = 'non';
            }
            echo '<pre>';
                print_r($form->getMessages());
            echo '</pre>';
        }
        
        $this->view->form = $form;
    }
}